package com.wang.work;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintStream;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Set;

/**
 * @author Administrator
 *	
		
	
	2、请写一个工具类DisposeFileUtil，该类有两个方法，一个是从硬盘中读取文件，
		一个是写出到硬盘一个文件
		1. writeToDisk(String fileName);
		2. readFromDisk(String fileName);
		文件名称自定义
	   测试类，只需要将两个方法完成功能后打印输出即可
	
	3、https://www.jianshu.com/p/2ad33ed2f72a	扩展
	   https://www.jb51.net/article/146336.htm	扩展
	   ConcurrentHashMap
	   红黑树
	
	4. 最简单方式将集合中的元素去重 new HashSet(Collection c)
		
	5. 将一个字符串 String str = "qqwwqqwwwwrrrewrewrerrtdgfgf";用最简单的方式去重复
	
	
 */
class DisposeFileUtil extends Tools<Properties>{

	@Override
	public boolean writeToDisk(String fileName, Properties p) {
		PrintStream printStream = null;
		try {
			printStream = new PrintStream(fileName);
			p.list(printStream);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			return false;
		} finally{
			if(printStream != null)
				printStream.close();
		}
		return true;
	}

	@Override
	public Properties readFromDisk(String fileName) {
		Properties properties = new Properties();
		FileInputStream fileInputStream = null;
		try {
			fileInputStream = new FileInputStream(fileName);
			properties.load(fileInputStream);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			return null;
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		} finally{
			try {
				if(fileInputStream != null)
					fileInputStream.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return properties;
	}


}
public class HomeWork {

	public static void main(String[] args) {
		getCharacterTimes("QWERTYUIQQQWJHDAADADBBBBCADA");
		getCharacterTimes("我的是我的1WERTYUIQQQWJHDAADADBBBBCADA");
		Properties hashMap = new Properties();
		hashMap.put("username", "admin");
		hashMap.put("password", "123456");
		DisposeFileUtil disposeFileUtil = new DisposeFileUtil();
		disposeFileUtil.writeToDisk("test.txt", hashMap);
		System.out.println(disposeFileUtil.readFromDisk("test.txt"));
		removeRepeat("qqwwqqwwwwrrrewrewrerrtdgfgf");
		removeRepeat2("qqwwqqwwwwrrrewrewrerrtdgfgf");
	}
	/**
	 * 5. 将一个字符串 String str = "qqwwqqwwwwrrrewrewrerrtdgfgf";用最简单的方式去重复
	 */
	public static void removeRepeat2(String str){
		String res = "";
		char[] charArray = str.toCharArray();
		for (int i = 0; i < charArray.length; i++) {
			if(res.indexOf(charArray[i]) == -1) res += charArray[i];
		}
		System.out.println(res);
	}
	public static void removeRepeat(String str){
		char[] charArray = str.toCharArray();
//		Character[] newArray = (Character[])charArray;
		LinkedHashSet linkedHashSet = new LinkedHashSet();
		for (int i = 0; i < charArray.length; i++) {
			linkedHashSet.add(charArray[i]);
		}
		Iterator iterator = linkedHashSet.iterator();
		String res = "";
		while(iterator.hasNext()){
			res += iterator.next();
		}
		System.out.println(res);
	}
	/**
	 * 
	 * 6. 有一个字符串，其中包含中文字符、英文字符和数字字符，请统计和打印出各
		个字符的个数。
	 * 1、有如下的一个字符串  String str = “QWERTYUIQQQWJHDAADADBBBBCAD”;  
		① 请统计出其中每一个字母出现的次数，在控制台打印如下的格式：map.containsKey(Key k)
			A----3
			B----4
			C----1
			D----2
		② 请打印出字母次数最多的那一对
			1. str.toCharArray()
			2. for if else
				map.containsKey(Key k)
				map.put()//第一次添加
				map.replace()//多次添加key-value
				
			3. values()
				Collections.max(values)
				map.containsValue();
	 */
	public static void getCharacterTimes(String str){
		if(str == null) return;
		char[] charArray = str.toCharArray();
		HashMap<Character,Integer> hashMap = new HashMap<Character,Integer>();
		for (int i = 0; i < charArray.length; i++) {
			if(hashMap.containsKey(charArray[i])){
				hashMap.replace(charArray[i], hashMap.get(charArray[i])+1);
			}else{
				hashMap.put(charArray[i], 1);
			}
		}
		System.out.println(hashMap);
		Collection<Integer> values = hashMap.values();
		Set<Entry<Character, Integer>> entrySet = hashMap.entrySet();
		for (Entry<Character, Integer> entry : entrySet) {
			if(entry.getValue() == Collections.max(values)){
				System.out.println(entry);
			}
		}
	}
	

}
